/**
 * 注册
 * @author  Jello
 * @create  2018/10/15 16:21
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/

var spinner = new Spinner(opts);
var token = ""; //获取验证码成功后返回的token
var identity = sessionStorage.getItem("upIdentity");

$(function() {

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var pwd = $("#pwd_input").val(); //密码

		var checkValue = (checkPhone(phoneNum) && checkPwd(pwd, 2));
		if(checkValue) {
			var param = {};
			param.phone = phoneNum;
			param.type = 0;
			//获取验证码接口上送
			gainCode(param, function(data) {
				token = data;
				//开启验证码定时器
				for(i = 1; i <= 60; i++) {
					if(i == 1) {
						window.setTimeout("countDown(" + i + ")");
					} else {
						window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
					}
				}
			});
		}
	})

	//注册
	$("#reg_btn").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var pwd = $("#pwd_input").val(); //密码
		var code = $("#code").val(); //验证码
		var checked = $("#checkbox").prop("checked"); //协议checkbox

		var flag = (checkPhone(phoneNum) && checkPwd(pwd, 2) && checkCode(code));
		if(flag) {
			if(!checked) {
				mui.toast("请阅读并勾选协议");
				return;
			}
			//校验验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 0;
			param.code = code;
			param.token = token;
			mateCode(param, function() {
				//注册
				$.ajax({
					type: "post",
					url: serverUrl + "/login/add_user",
					data: "{\"upIdentity\":\"" + identity + "\",\"phone\":\"" + phoneNum + "\",\"password\":\"" + hex_md5(pwd) + "\",\"type\":\"" + 0 + "\"}",
					async: false,
					cache: false,
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					timeout: 15000,
					crossDomain: true,
					success: function(data) {
//						console.log("注册接口：" + JSON.stringify(data));
						spinner.spin();
						var registObj = eval(data);
						if(registObj.code == 0 && registObj.data == 0) {
							mui.toast("注册成功");
							setTimeout(function() {
								var id = getParam('detailId');
								if(id != ""){
									window.location.href = "login.html?id=" + id ;
								}else{
									window.location.href = "login.html";
								}
								
							}, 1000);
						} else {
							mui.toast(registObj.msg);
							return;
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						spinner.spin();
						if(status == 'timeout') {
							mui.toast("请求超时");
						} else {
							mui.toast("请求网络服务数据失败");
						}
						return;
					}
				});
			})
		}
	})
})

/**
 * 输入密码切换显示 
 */
document.getElementById("pwd_input_show").addEventListener("tap", function() {
	var type = $("#pwd_input").prop("type")
	if("password" == type) {
		$("#pwd_input").prop("type", "text");
		$("#pwd_input_show").attr("src", "img/login/regist_03.jpg");
	} else {
		$("#pwd_input").prop("type", "password");
		$("#pwd_input_show").attr("src", "img/login/login_22.jpg");
	}
}, false)